var isEdit;
var lastRowID;

$(function() {
	var parent = getIframeSize();
	scwidth =  parent.width-1;
	scheight = parent.height-74;
	
	init();
	
	brandjqgird();
	
});

function init(){
	isEdit = false;
	lastRowID = "";
}

function brandjqgird(){
	var dateSearchOptions = {
			sopt:['eq','ne','lt','le','gt','ge']
		}
	//带有初始化日期的
	var dateSearchOptions1 = {
				sopt:['eq','ne','lt','le','gt','ge'],
				dataInit:function(jElem) { 
					jElem.datepicker({
						beforeShow:function(i,e){e.dpDiv.css('z-index',105); }
					});
				} 
	}
	
	$("#brandtable").jqGrid({        	    	
    	url: "brand.do?type=show",
    	mtype: 'POST',	   
    	datatype: "json",	//更改使用json数据   
    	width: scwidth,
    	height: scheight,    	
    	colNames: ['品牌ID','品牌名称','店铺地址','品牌图标','联系人','联系电话','是否有效','楼层','显示顺序','最后更新时间'],
    	colModel: [
    		{name:'BRAND_ID',index:'BRAND_ID',align:'center',hidden:true,search:false},    		
    		{name:'BRAND_NAME',index:'BRAND_NAME',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_ADDRESS',index:'BRAND_ADDRESS',align:'left',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_LOGO',index:'BRAND_LOGO',align:'left',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_CONTACT',index:'BRAND_CONTACT',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_TEL',index:'BRAND_TEL',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_ACTIVE',index:'BRAND_ACTIVE',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_FLOOR',index:'BRAND_FLOOR',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_ORDER',index:'BRAND_ORDER',align:'center',editable:true,searchoptions:dateSearchOptions},
    		{name:'BRAND_UPDATE_TIME',index:'BRAND_UPDATE_TIME',align:'center',editable:true,searchoptions:dateSearchOptions1,editoptions:{dataInit:function(jElem) {$( jElem).datepicker({
						beforeShow:function(i,e){e.dpDiv.css('z-index',999); }
					}); }}}		
    	],
    	pager: 'brandpager',		//分页工具栏
    	sortname: 'BRAND_ID',	//排序
    	loadonce:false,			//每次都从服务器加载数据
    	rownumbers: true,    	//序列
    	gridview: true,			//提高加载速度
    	sortorder: "asc",    	
    	imgpath: 'images', 		//图片存放路径  
    	rowNum: 25,				//每页显示记录数
    	viewrecords: true,     	//是否显示行数
    	jsonReader:{  
            repeatitems : false, 	//指明每行的数据是可以重复的，如果设为false，则会从返回的数据中按名字来搜索元素，这个名字就是colModel中的名字
            id : "0"
        },         
    	rowList:[25,50,100],		//可调整每页显示的记录数
    	multiselect: true, 			//是否支持多选  
    	editurl:"brand.do?type=add",
    	caption: "商铺列表",
    	onSelectRow:function(id){
    		if(!isEdit){
    			return false;
    		}
    		//如果勾选复选框，则不编辑
    		if(event.srcElement && event.srcElement.nodeName ==="INPUT" &&  event.srcElement.type ==="checkbox" && event.srcElement.className.indexOf("cbox") >=0)
	   		{
	   			
	   			saveUnfinishTable();
	   			return false;
	   		}else{
	   			if(id && id!==lastRowID){
	   				if(lastRowID) {						
						$('#brandtable').saveRow(lastRowID,null,"clientArray",null);	
					}
					$('#brandtable').editRow(id,true,onEdit(id),null,"clientArray",null);
					
	   			}else if(id && id == lastRowID){
	   				$('#brandtable').editRow(id,true,onEdit(id),null,"clientArray",null);
	   			}
	   			
	   			function onEdit(rowid){
					$.each($("#brandtable select.editable option"),function(i,n){
						if(!$(n).attr("title"))
						{
							if($(n).text()){$(n).attr("title",$(n).text());}
						}
					});
											
				}
	   			
	   			lastRowID = id;
	   			
	   			$("tbody tr", $("#brandtable")).removeClass("ui-state-highlight");
				$("[id^=jqg_]",$("#brandtable")).attr("checked",false);
				$("#cb_jqg",$("#brandtable").parent().parent()).attr("checked",false);
				$("#brandtable").setGridParam("selarrrow",[]);
	   		}
	   		return false;
	   	},
	    loadComplete: function() { 	  	 
	    	$("#ui-datepicker-div").hide();
	    	
	    	if(!isEdit){
	    		$("#br_save").readonly();
	    		$("#br_delete").readonly();
	    		$("#add_brandtable").readonly();
	    	}
	    } 
    }).navGrid('#brandpager',{search:p_br_search,refresh:p_br_refresh,edit:false,add:p_br_add,del:false,searchtext:"查找",refreshtext:"刷新",addtext:"添加"},{},{},{},{multipleSearch:true});
    
     if(p_br_delete){
    	 jQuery("#brandtable").navButtonAdd("#brandpager",{
    	 	id : "br_delete",
			caption : "删除",
			title: '删除店铺',
			buttonicon : 'ui-icon-trash',
			onClickButton: deleteBrand,
			position : "first",
			cursor : 'pointer'
		});
    }
    
    if(p_br_save){
    	 jQuery("#brandtable").navButtonAdd("#brandpager",{
    	 	id : "br_save",
			caption : "保存",
			title: '保存店铺',
			buttonicon : 'ui-icon-disk',
			onClickButton: saveBrand,
			position : "first",
			cursor : 'pointer'
		});
    }
    
   
    
    /*if(p_br_add){
    	 jQuery("#brandtable").navButtonAdd("#brandpager",{
    	 	id : "br_add",
			caption : "增加",
			title: '增加店铺',
			buttonicon : 'ui-icon-plus',
			onClickButton: addBrand,
			position : "first",
			cursor : 'pointer'
		});
    }*/
    
    if(p_br_update){
    	 jQuery("#brandtable").navButtonAdd("#brandpager",{
    	 	id : "gs_update",
			caption : "编辑",
			title: '编辑店铺',
			buttonicon : 'ui-icon-pencil',
			onClickButton: updateBrand,
			position : "first",
			cursor : 'pointer'
		});
    }
    
}


//添加商品
function addBrand(){
	
}

//删除商品
function deleteBrand(){
	var selectedRowID = $("#brandtable").getGridParam("selarrrow");
	if(selectedRowID.length > 0){		
		if(window.confirm("确认删除?")){
			var postData = {rowID:selectedRowID};
				$.ajax({
					type: 'POST',
					url:  "brand.do?type=deleteBrand",
					dataType: "json",
					data: postData,
					success: function(data){
						var result = data;
						alert(result.msg);
						$("#brandtable").setGridParam({url: "brand.do?type=show"}).trigger("reloadGrid");
			    	},
			    	error: function(data){
		           		alert('删除失败！');
		        	}
				});
		}
	}else{
		alert("请选择要删除的行");
		return false;
	}
}

//编辑商品
function updateBrand(){
	if(!isEdit){
		isEdit = true;
		$("#br_save").readonly(false);
		$("#br_delete").readonly(false);
		$("#add_brandtable").readonly(false);
	}else{
		isEdit = false;
		$("#br_save").readonly();
		$("#br_delete").readonly();
		$("#add_brandtable").readonly();
		saveUnfinishTable();
	}
}

//保存商品
function saveBrand(){
	saveUnfinishTable();
	var postData = {
		BRAND_ID:[],
		BRAND_NAME:[],
		BRAND_ADDRESS:[],
		BRAND_LOGO:[],
		BRAND_CONTACT:[],
		BRAND_TEL:[],
		BRAND_ACTIVE:[],
		BRAND_FLOOR:[],
		BRAND_ORDER:[],
		BRAND_UPDATE_TIME:[]
	}
	
	var ids = $("#brandtable").getDataIDs();
	
	$.each(ids,function(i,n){	
		var rowData = $("#brandtable").getRowData(n);
		postData.BRAND_ID[i] = rowData.BRAND_ID,
		postData.BRAND_NAME[i] = rowData.BRAND_NAME,
		postData.BRAND_ADDRESS[i] = rowData.BRAND_ADDRESS,
		postData.BRAND_LOGO[i] = rowData.BRAND_LOGO,
		postData.BRAND_CONTACT[i] = rowData.BRAND_CONTACT,
		postData.BRAND_TEL[i] = rowData.BRAND_TEL,
		postData.BRAND_ACTIVE[i] = rowData.BRAND_ACTIVE,
		postData.BRAND_FLOOR[i] = rowData.BRAND_FLOOR,
		postData.BRAND_ORDER[i] = rowData.BRAND_ORDER,
		postData.BRAND_UPDATE_TIME[i] = rowData.BRAND_UPDATE_TIME
	});
	
	$.ajax({
		type: 'POST',
		url:  "brand.do?type=saveBrand",
		dataType: "json",
		data: postData,
		success: function(data){
			var result = data;
			alert(result.msg);
			$("#brandtable").setGridParam({url: "brand.do?type=show"}).trigger("reloadGrid");
    	},
    	error: function(data){
          		alert('保存失败！');
       	}
	});
}

//客户端保存
function saveUnfinishTable()
{
	var ids = $("#brandtable").getDataIDs();
	$.each(ids,function(i,id){
		ind = $("#brandtable").getInd(id,true);
		editable = $(ind).attr("editable");
		if(editable === "1")
		{
			$('#brandtable').saveRow(id,null,"clientArray",null);
		}
	});
	
}
